package com.six.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.six.pojo.Dept;
import com.six.pojo.dto.DeptDto;
import com.six.pojo.vo.DeptVo;
import com.six.result.Result;
import com.six.result.ResultUtils;
import com.six.service.DeptService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口文档")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 查询所有带分页
     * @param deptVo
     * @return
     */
    @GetMapping("/list")
    public Result list(DeptVo deptVo) {
        Page<Dept> page = new Page<>(deptVo.getPage(), deptVo.getLimit());
        Page<DeptDto> deptList = deptService.findAllByPageAndCondition(page,deptVo);
        return ResultUtils.buildSuccess(deptList);
    }

    /**
     * 树状效果
     * @return Dept
     */
    @GetMapping("/selectTree")
    public Result selectDeptTree() {
        List<Dept> deptList =  deptService.selectDeptTree();
        return ResultUtils.buildSuccess(deptList);
    }

    @PostMapping("/add")
    public Result add(Dept dept) {
        deptService.save(dept);
        return ResultUtils.buildSuccess();
    }
    @GetMapping("/getDeptById/{id}")
    public Result getDeptById(@PathVariable("id") Long id) {
        Dept dept = deptService.getById(id);
        return ResultUtils.buildSuccess(dept);
    }
    @PutMapping("/update")
    public Result update(Dept dept) {
        deptService.updateById(dept);
        return ResultUtils.buildSuccess();
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        deptService.removeById(id);
        return ResultUtils.buildSuccess();
    }
}
